/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.media.image.PixelMap;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.binary.ConnectedAreaLabel;
import com.cv4j.core.binary.MorphOpen;
import com.cv4j.core.binary.Threshold;
import com.cv4j.core.datamodel.ByteProcessor;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.Rect;
import com.cv4j.core.datamodel.Size;
import com.cv4j.image.util.Preconditions;
import com.cv4j.image.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * 文本分离与切割
 *
 * @since 2021-05-17
 */
public class MorphologyAbilitySlice extends BaseAbilitySlice {
    private Image image0;
    private Image image1;
    private Image image2;
    private Image image3;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_morphology;
    }

    @Override
    protected void initComponents(Intent intent) {
        image0 = (Image) findComponentById(ResourceTable.Id_image0);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image3 = (Image) findComponentById(ResourceTable.Id_image3);

        PixelMap pixelMap = PixelMapUtil.getPixelMapFromResource(getContext(), ResourceTable.Media_test_binary1).get();
        image0.setPixelMap(pixelMap);

        CV4JImage cv4jimage = new CV4JImage(pixelMap);
        Threshold threshold = new Threshold();
        final int colorsize = 255;
        threshold.process((ByteProcessor) cv4jimage.convert2Gray().getProcessor(),
            Threshold.THRESH_TRIANGLE, Threshold.METHOD_THRESH_BINARY_INV, colorsize);
        image1.setPixelMap(cv4jimage.getProcessor().getImage().toBitmap());

        MorphOpen morphOpen = new MorphOpen();
        cv4jimage.resetBitmap();
        final int five = 5;
        morphOpen.process((ByteProcessor) cv4jimage.getProcessor(), new Size(five));
        image2.setPixelMap(cv4jimage.getProcessor().getImage().toBitmap());

        ConnectedAreaLabel connectedAreaLabel = new ConnectedAreaLabel();
        int[] mask = new int[cv4jimage.getProcessor().getWidth() * cv4jimage.getProcessor().getHeight()];
        List<Rect> rectangles = new ArrayList<>();
        connectedAreaLabel.process((ByteProcessor) cv4jimage.getProcessor(), mask, rectangles, true);
        cv4jimage.resetBitmap();
        PixelMap newBitmap = cv4jimage.getProcessor().getImage().toBitmap();
        if (Preconditions.isNotBlank(rectangles)) {
            Tools.drawRects(newBitmap, rectangles);
        }
        image3.setPixelMap(newBitmap);
    }
}
